using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using NUnit.Framework;
using Poka_Yoke.Analysis;

namespace Poka_Yoke.UnitTests
{
    [TestFixture]
    public class FileHelperTests
    {
        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetLineNumberWithTooSmallMatchLocation()
        {
            FileHelper.CalculateLineNumber(-1, "Testing 1234");
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetLineNumberWithMatchAfterEndOfString()
        {
            FileHelper.CalculateLineNumber(30, "Testing");
        }

        [Test]
        public void TestGetLineWithZeroMatchLocation()
        {
            Assert.AreEqual(1, FileHelper.CalculateLineNumber(0, "Test"));
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetLineWithNullMessage()
        {
            FileHelper.CalculateLineNumber(0, null);
        }

        [Test]
        public void TestGetLineWithSmallString()
        {
            Assert.AreEqual(4,FileHelper.CalculateLineNumber(6, "\r\n\r\n\r\nTest"));
        }

        [Test]
        public void TestGetLineWithLargerString()
        {
            Assert.AreEqual(6, FileHelper.CalculateLineNumber(39, "Test1\r\nTest2\r\nTest3\r\nTest4\r\nTest5\r\nTest6\r\nTest7\r\n"));
        }
        
        [Test]
        public void TestGetLineWithSmallestString()
        {
            Assert.AreEqual(1, FileHelper.CalculateLineNumber(0, "m"));
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetFileContentsWithNullParameters()
        {
            FileHelper.LoadFile(null);
        }

        [Test]
        [ExpectedException(typeof(FileNotFoundException))]
        public void TestGetFileContentsWithNonExistantFile()
        {
            string fileName = Path.GetTempFileName();

            File.Delete(fileName);

            FileHelper.LoadFile(fileName);
        }

        [Test]
        public void TestGetFileContentsWithValidParameters()
        {
           string fileName = Path.GetTempFileName();

           StreamWriter writer = new StreamWriter(File.OpenWrite(fileName));

           string testString = "Testing123";
           
           writer.Write(testString);

           writer.Close();
 
           Assert.AreEqual(testString, FileHelper.LoadFile(fileName));

           File.Delete(fileName);

        }
    }
}
